<?php
/**
 * Created by PhpStorm.
 * User: mr.houhai
 * Date: 2018/2/26
 * Time: 0:01
 */

namespace Xcx\Model;

class GoodsOrderDiscountModel extends BaseModel {

    /**
     * 添加折扣信息
     * @param $orderNo
     * @param $type
     * @param $pid
     * @return mixed
     */
    public function addOrderDiscount($orderNo, $type, $pid)
    {
        $discount = 0;
        switch ($type) {
            case 1:
                $discount = $this->getCouponDiscount($pid);
                break;
            case 2:
                $discount = $this->getBargainDiscount($pid);
                break;
            default:
                break;
        }

        $data['order_no'] = $orderNo;
        $data['discount'] = $discount;
        $data['pid'] = $pid;
        $data['type'] = $type;
        $data['add_time'] = time();
        //防止用户提交过订单但未支付成功，首先删除该订单历史折扣信息，再添加新的折扣信息
        $this->where(array('order_no'=>$orderNo))->delete();
        $res = $this->data($data)->add();
        if ($res) {
            return $discount;
        } else {
            return false;
        }
    }

    /**
     * 获取优惠券折扣
     * @param $id
     * @return mixed
     */
    private function getCouponDiscount($id)
    {
        $UserCoupon = new UserCouponModel();
        $data = $UserCoupon->getUserCouponInfo($id);
        return $data['price'];
    }

    /**
     * 获取砍价折扣
     * @param $id
     * @return mixed
     */
    private function getBargainDiscount($id)
    {
        $BargainDetails = new GoodsBargainDetailsModel();
        return $BargainDetails->getBargainCount($id);
    }

}